package com.company.cloud.club.modules.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.cloud.club.modules.constants.AliyunOSSConstant;
import com.company.cloud.club.modules.constants.FileType;
import com.company.cloud.club.modules.db.dao.FileInfoMapper;
import com.company.cloud.club.modules.db.domain.FileInfo;
import com.company.cloud.club.modules.service.FileInfoService;
import com.company.cloud.club.modules.utils.FileUtil;
import com.company.cloud.club.modules.utils.UploadVideoUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;

@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {

    @Autowired
    private FileInfoMapper fileInfoMapper;

    @Override
    public String uploadImage(Integer userId, MultipartFile file) {
        String path = "assets/image/";
        String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename();
        long size = file.getSize();
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            if (size > 600 * 1024) {
                float outputQuality = 1f;
                if (size > 8192 * 1024) {
                    outputQuality = 0.5f;
                } else if (size > 4096 * 1024) {
                    outputQuality = 0.75f;
                }
                Thumbnails.of(file.getInputStream())
                        .scale(1f)
                        .outputQuality(outputQuality)
                        .outputFormat("jpg")
                        .toOutputStream(outputStream);
                FileUtil.uploadByBytes(AliyunOSSConstant.BUCKET_NAME_PUBLIC, path, filename, outputStream.toByteArray());
            } else {
                FileUtil.uploadByBytes(AliyunOSSConstant.BUCKET_NAME_PUBLIC, path, filename, file.getBytes());
            }
            return AliyunOSSConstant.OSS_HOST + path + filename;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String uploadAudio(Integer userId, MultipartFile file) {
        try {
            String path = "assets/audio/";
            String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            FileUtil.uploadByBytes(AliyunOSSConstant.BUCKET_NAME_PUBLIC, path, filename, file.getBytes());
            return AliyunOSSConstant.OSS_HOST + path + filename;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String uploadVideo(Integer userId, MultipartFile file) {
        try {
            String path = "assets/video/";
            String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            FileUtil.uploadByBytes(AliyunOSSConstant.BUCKET_NAME_PUBLIC, path, filename, file.getBytes());
            return AliyunOSSConstant.OSS_HOST + path + filename;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String uploadWgt(Integer currentUserId, MultipartFile file) {
        try {
            String path = "assets/wgt/";
            String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            FileUtil.uploadByBytes(AliyunOSSConstant.BUCKET_NAME_PUBLIC, path, filename, file.getBytes());
            return AliyunOSSConstant.OSS_HOST + path + filename;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
